<?php
require 'vendor/autoload.php';

class TwitterPostsManager{

	private $trendManager;
	private $textCleaner;
	private $twitterXML;
	private $webURL;

	function __construct($trendManager, $textCleaner){

		$this->trendManager = $trendManager;
		$this->textCleaner = $textCleaner;
	}

	function setTwitterXML($twitterXML){

		$this->twitterXML = $twitterXML;
	}

	function setWebURL($webURL){

		$this->webURL = $webURL;
	}

	function generateRSSFeed(){

		$this->trendManager->getTrendingWords();
		$trendingNewsArray = $this->trendManager->getTrendingNews();

		$rssfeed = '<?xml version="1.0" encoding="UTF-8"?>';
		$rssfeed .= '<rss version="2.0">';
		$rssfeed .= '<channel>';

		foreach ($trendingNewsArray as $news) {

			$title = $news->getTitle();

			$rssfeed .= '<item>';
			$rssfeed .= '<title>' . $title . '</title>';
			$filteredTitle = $this->textCleaner->removeUnwantedWords($title);
			$link = $this->webURL . '/buscar.php?keyword=' . $filteredTitle . '&ordenamiento=peso' . '&theater=' . $news->getLink();
			$link = htmlspecialchars($link);	
			$rssfeed .= '<link>' . $link . '</link>';
			$rssfeed .= '</item>';

		}

		$rssfeed .= '</channel>';
   		$rssfeed .= '</rss>';

		return $rssfeed;
	}

	function saveRSSFeedToFile($rssfeed){

		file_put_contents($this->twitterXML, $rssfeed);
	}

}

?>
